

import { dictItems, paramConfig } from "@/api/system/dict";

const dictMap = {};
const paramMap = {};

/**
 * 获取字典项Array
 * @param {String} dictCode 字典编码
 * @param {String} dictType 字典类型，dict普通字典，tableDict表数据字典
 */
export function getDictItems(dictCode, dictType) {
  dictType = dictType=='tableDict'?'tableDict':'dict';
  let itemArray = dictType == 'dict' ? dictMap[dictCode+'-'+dictType] : null;
  if(!itemArray || !itemArray.length){
    return dictItems(dictCode, dictType).then(res => {
      itemArray = res.data;
      //dictMap[dictCode+'-'+dictType] = itemArray;
      return itemArray;
    }).catch(()=>{
      return [];
    });
  }
  return Promise.resolve(itemArray);
}

export function getParamConfig(key){
  let value = paramMap[key];
  if(!value && value != 0){
    return paramConfig(key).then(res => {
      value = res.data;
      //paramMap[key] = value;
      return value;
    }).catch(()=>{
      return '';
    });
  }
  return Promise.resolve(value);
}
